package com.yfk.base.http;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * 推送数据到统一认证平台
     * @param apiURL
     * @param param
     * @return
     * @throws Exception
     */
    public static String sendUamPost(String apiURL, String param,String type) throws Exception {
        URL url = new URL(apiURL);
        URLConnection uc = url.openConnection();
        uc.setDoOutput(true);
        uc.setUseCaches(false);
        uc.setRequestProperty("Content-Type","application/json");
        uc.setRequestProperty("Charset", "UTF-8");
//        uc.setRequestProperty("Content-Length", "10000");
        HttpURLConnection hc = (HttpURLConnection) uc;
        hc.setRequestMethod(type);
        hc.setRequestProperty("Charsert", "UTF-8");
        OutputStream os = null;
        DataOutputStream dos = null;
        String result = "";
        try {
            os = hc.getOutputStream();
            dos = new DataOutputStream(os);
            dos.write(param.getBytes());
            dos.flush();
            BufferedReader bReader = new BufferedReader(new InputStreamReader(hc.getInputStream(),"UTF-8"));
            int ch;
            while ((ch = bReader.read()) != -1) {
                result += String.valueOf((char) ch);
            }
        }catch(Exception e) {
            logger.error("sendUamPost error:"+ e.getMessage());
            throw new Exception("sendUamPost error:"+ e.getMessage());
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (Exception e2) {
                    logger.error("sendUamPost error-dos:"+ e2.getMessage());
                    logger.error(e2.getMessage(), e2);
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e2) {
                    logger.error("sendUamPost error-os"+ e2.getMessage());
                }
            }
            if(!"".equals(result)&&result!=null) {
                JSONObject parseObject = JSONObject.parseObject(result);
                Object object = parseObject.get("code");
                if(!"".equals(object)&&object!=null) {
                    int parseInt = Integer.parseInt(object.toString());
                    if(parseInt == 0) {
                        logger.error("sendUamPost error-dos:"+ param+"返回值"+result);
                        return result;
                    }
                }
                String message =apiURL+";;type:"+type+";;;数据:"+param+";;;返回值"+result;
                logger.info(message);
            }else {
                String message =apiURL+";;type:"+type+";;;数据:"+param+";;;返回值"+result;
                logger.info(message);
            }
        }
        return  result;
    }

}